package com.jbp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.dao.PoolRoomDao;
import com.jbp.model.PoolRoom;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class PoolRoomService extends ServiceImpl<PoolRoomDao, PoolRoom> implements IService<PoolRoom> {


    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PoolRoom> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null) {
            queryWrapper.like("id",params.get("id"));
        }
        if (params.get("roomId") != null) {
            queryWrapper.like("room_id",params.get("roomId"));
        }
        if (params.get("rate") != null) {
            queryWrapper.like("rate",params.get("rate"));
        }
        if (params.get("maxGift") != null) {
            queryWrapper.like("max_gift",params.get("maxGift"));
        }
        if (params.get("createTime") != null) {
            queryWrapper.like("create_time",params.get("createTime"));
        }
        if (params.get("updateTime") != null) {
            queryWrapper.like("update_time",params.get("updateTime"));
        }
        if (params.get("fromDate")!=null) {
            queryWrapper.ge("create_time", params.get("fromDate"));
        }
        if (params.get("toDate")!=null) {
            queryWrapper.le("DATE_FORMAT(create_time,   '%Y-%m-%d')", params.get("toDate"));
        }
        queryWrapper.orderByDesc("id");
        IPage<PoolRoom> page = this.page(
            new Query<PoolRoom>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }

    public PoolRoom getByRoomId(Integer roomId) {
        QueryWrapper<PoolRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        return getOne(queryWrapper);
    }

    public PoolRoom getPoolRoomByUserId(Integer userId) {
//        QueryWrapper<PoolRoom> queryWrapper = new QueryWrapper<>();
//        queryWrapper.inSql("room_id","SELECT du.room_id from user_daily_sendgift_record du where du.from_user_id = "+userId+" and du.type = 'shop' ORDER BY du.update_time ASC LIMIT 1");
//        return getOne(queryWrapper);
        return baseMapper.getPoolRoomByUserId(userId);
    }
}